/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import org.jvnet.substance.SubstanceLookAndFeel;
import otros.Configuracion;
import otros.Util;

/**
 *
 * @author FERRETERIA
 */
public class Principal extends javax.swing.JFrame {

    static FArtXCliente ac;
    FReguArtFami sf;
    FInformeArt ia;
    FVentas v;
    static FListArticulos la;
    FArticulo a;
    FFamilia f;
    FStock s;
    FClientes c;
    FProveedores prov;
    FCompras comp;
    static FFacturas fac;
    FTPV tpv;
    static FFacturasCompras facomp;
    private String mensaje = "La ventana ya esta abierta...";
    
    public Principal() {        
        initComponents();
        String max = Configuracion.getInstance().getProperty(Configuracion.MAXIMIZADO);
        if (max != null)
            this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setIconImage(new ImageIcon(getClass().getResource("/img/icono2.png")).getImage()); 
    }
    
    public static JDesktopPane getPanel() {
        return panel;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem5 = new javax.swing.JMenuItem();
        panel = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mArtAgregar = new javax.swing.JMenuItem();
        mArtBuscar = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenu2 = new javax.swing.JMenu();
        mFamCrear = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        mStock = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        mCliGestionar = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        abrirTPV = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        mFacturas = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        mProveedores = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        mCompFacturas = new javax.swing.JMenuItem();
        mIntroducirFacturas = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenuItem5.setText("jMenuItem5");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Gestion");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(808, 600));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 808, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 391, Short.MAX_VALUE)
        );

        jMenu1.setText("Articulos");

        mArtAgregar.setText("Agregar");
        mArtAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mArtAgregarActionPerformed(evt);
            }
        });
        jMenu1.add(mArtAgregar);

        mArtBuscar.setText("Buscar");
        mArtBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mArtBuscarActionPerformed(evt);
            }
        });
        jMenu1.add(mArtBuscar);

        jMenuItem4.setText("Informe por Familia");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);
        jMenu1.add(jSeparator1);

        jMenu2.setText("Familias");

        mFamCrear.setText("Crear");
        mFamCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mFamCrearActionPerformed(evt);
            }
        });
        jMenu2.add(mFamCrear);

        jMenu1.add(jMenu2);
        jMenu1.add(jSeparator2);

        mStock.setText("Stock por Articulo");
        mStock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mStockActionPerformed(evt);
            }
        });
        jMenu1.add(mStock);

        jMenuItem6.setText("Stock por Familia");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Clientes");

        mCliGestionar.setText("Gestionar");
        mCliGestionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mCliGestionarActionPerformed(evt);
            }
        });
        jMenu3.add(mCliGestionar);

        jMenuBar1.add(jMenu3);

        jMenu5.setText("Ventas");

        abrirTPV.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, 0));
        abrirTPV.setText("TPV");
        abrirTPV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirTPVActionPerformed(evt);
            }
        });
        jMenu5.add(abrirTPV);
        jMenu5.add(jSeparator4);

        mFacturas.setText("Gestion de Facturas/Albaranes");
        mFacturas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mFacturasActionPerformed(evt);
            }
        });
        jMenu5.add(mFacturas);

        jMenuBar1.add(jMenu5);

        jMenu6.setText("Compras");

        mProveedores.setText("Gestion de Proveedores");
        mProveedores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mProveedoresActionPerformed(evt);
            }
        });
        jMenu6.add(mProveedores);
        jMenu6.add(jSeparator3);

        mCompFacturas.setText("Gestion de Facturas/Albaranes");
        mCompFacturas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mCompFacturasActionPerformed(evt);
            }
        });
        jMenu6.add(mCompFacturas);

        mIntroducirFacturas.setText("Insertar Facturas/Albaranes");
        mIntroducirFacturas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mIntroducirFacturasActionPerformed(evt);
            }
        });
        jMenu6.add(mIntroducirFacturas);

        jMenuBar1.add(jMenu6);

        jMenu7.setText("Utilidades");

        jMenuItem3.setText("Cierre Caja");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem3);

        jMenuItem8.setText("347");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem8);

        jMenuItem1.setText("Movimiento de articulo");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem1);

        jMenuItem7.setText("Articulos por Cliente");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem7);

        jMenuItem2.setText("Estadisticas");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem2);

        jMenuBar1.add(jMenu7);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //METODO QUE DEVUELVE UN VALOR BOOLEAN PARA SABER SI UN JINTERNALFRAME ESTA ABIERTO O NO
    public static  boolean estacerrado(Object obj){
        JInternalFrame[] activos=panel.getAllFrames();
        boolean cerrado=true;
        int i=0;
        while (i<activos.length && cerrado){
            if(activos[i]==obj){
                    cerrado=false;
            }
            i++;
        }
        return cerrado;
    }
    
    public static void centrarFrame (JInternalFrame f) {
        Dimension panelSize = panel.getSize();
        Dimension fSize = f.getSize();
        f.setLocation((int)(panelSize.getWidth() - fSize.getWidth())/2,
                            (int)(panelSize.getHeight() - f.getHeight())/2);
            
    }
        
    private void mArtBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mArtBuscarActionPerformed
        if(estacerrado(la)){
            la = new FListArticulos();
            panel.add(la);
            centrarFrame(la);
            la.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }       
    }//GEN-LAST:event_mArtBuscarActionPerformed

    public void abrirArticulo () {
        if(estacerrado(a)){
            a = new FArticulo();
            panel.add(a);        
            centrarFrame(a);
            a.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }
    
    private void mArtAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mArtAgregarActionPerformed
        abrirArticulo();       
    }//GEN-LAST:event_mArtAgregarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        Util.setConfiguracionLog(Util.LOG4_FICH);
        //SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.BusinessSkin");       
        SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.RavenSkin");       
    }//GEN-LAST:event_formWindowOpened

    private void mFamCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mFamCrearActionPerformed
        if(estacerrado(f)){
            f = new FFamilia();
            panel.add(f);        
            centrarFrame(f);
            f.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mFamCrearActionPerformed

    private void mStockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mStockActionPerformed
        if(estacerrado(s)){
            s = new FStock();
            panel.add(s);        
            centrarFrame(s);
            s.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mStockActionPerformed

    private void mCliGestionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mCliGestionarActionPerformed
         if(estacerrado(c)){
            c = new FClientes();
            panel.add(c);        
            centrarFrame(c);
            c.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mCliGestionarActionPerformed

    private void abrirTPVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirTPVActionPerformed
        //if(estacerrado(tpv)){
            tpv = new FTPV();
            panel.add(tpv);        
            centrarFrame(tpv);
            tpv.setVisible(true);
            tpv.getTxtBuscar().requestFocus();
//        }
//        else{
//            JOptionPane.showMessageDialog(this,mensaje);
//        }
    }//GEN-LAST:event_abrirTPVActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        for (int i = 0; i < panel.getComponentCount(); i++)
            ((JInternalFrame)panel.getComponent(i)).dispose();
    }//GEN-LAST:event_formWindowClosing

    private void mFacturasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mFacturasActionPerformed
        if(estacerrado(fac)){
            fac = new FFacturas();
            panel.add(fac);        
            centrarFrame(fac);
            fac.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mFacturasActionPerformed

    private void mProveedoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mProveedoresActionPerformed
       if(estacerrado(prov)){
            prov = new FProveedores();
            panel.add(prov);        
            centrarFrame(prov);
            prov.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mProveedoresActionPerformed

    private void mIntroducirFacturasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mIntroducirFacturasActionPerformed
        if(estacerrado(comp)){
            comp = new FCompras();
            panel.add(comp);        
            centrarFrame(comp);
            comp.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mIntroducirFacturasActionPerformed

    private void mCompFacturasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mCompFacturasActionPerformed
        if(estacerrado(facomp)){
            facomp = new FFacturasCompras();
            panel.add(facomp);        
            centrarFrame(facomp);
            facomp.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_mCompFacturasActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        if(estacerrado(v)){
            v = new FVentas();
            panel.add(v);        
            centrarFrame(v);
            v.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        if(estacerrado(ia)){
            ia = new FInformeArt();
            panel.add(ia);        
            centrarFrame(ia);
            ia.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        if(estacerrado(sf)){
            sf = new FReguArtFami();
            panel.add(sf);        
            centrarFrame(sf);
            sf.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this,mensaje);
        }
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        F347 p = new F347();
        panel.add(p);
        centrarFrame(p);
        p.setVisible(true);
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        FFactXProduc p = new FFactXProduc();
        panel.add(p);
        centrarFrame(p);
        p.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        FEstadisticas p = new FEstadisticas();
        panel.add(p);
        centrarFrame(p);
        p.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        ac = new FArtXCliente();
        panel.add(ac);
        centrarFrame(ac);
        ac.setVisible(true);
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */ 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                setDefaultLookAndFeelDecorated(true);     
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem abrirTPV;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JMenuItem mArtAgregar;
    private javax.swing.JMenuItem mArtBuscar;
    private javax.swing.JMenuItem mCliGestionar;
    private javax.swing.JMenuItem mCompFacturas;
    private javax.swing.JMenuItem mFacturas;
    private javax.swing.JMenuItem mFamCrear;
    private javax.swing.JMenuItem mIntroducirFacturas;
    private javax.swing.JMenuItem mProveedores;
    private javax.swing.JMenuItem mStock;
    public static javax.swing.JDesktopPane panel;
    // End of variables declaration//GEN-END:variables
}
